package com.tarena.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;

import com.tarena.dao.MeettingroomDao;
import com.tarena.entity.Meettingroom;
import com.tarena.service.MeettingroomService;

@Service
public class MeettingroomServiceImpl implements MeettingroomService {
	@Resource
	MeettingroomDao mrs;

	public boolean save(Meettingroom meettingroom) {
		Integer sum = mrs.getSum();
		mrs.save(meettingroom);
		if (sum < mrs.getSum()) {
			return true;
		}
		return false;
	}

	public boolean delete(Integer id) {
		Integer sum = mrs.getSum();
		mrs.delete(id);
		if (sum > mrs.getSum()) {
			return true;
		}
		return false;
	}

	public void update(Meettingroom meettingroom) {
		mrs.update(meettingroom);

	}

	public List<Meettingroom> findById(Integer id) {
		return mrs.findById(id);
	}

	public List<Meettingroom> findAllMeettingroom() {
		return mrs.findAllMeettingroom();
	}

	public Integer getSum() {
		return mrs.getSum();
	}

	public List<Meettingroom> findByPage(int offset, int limit,
			Meettingroom meettingroom) throws Exception {
		RowBounds rb = new RowBounds(offset, limit);
		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				"applicationContext.xml");
		System.out.println("meettingroom.getSeatNum()"
				+ meettingroom.getSeatNum());

		SqlSessionFactory sf = (SqlSessionFactory) ctx
				.getBean(SqlSessionFactory.class);
		SqlSession session = sf.openSession();
		List<Meettingroom> list = session.selectList("findMeettingroomByParam",
				meettingroom, rb);
		session.close();
		return list;
	}

	public List<Meettingroom> findByPage(int offset, int limit)
			throws Exception {
		RowBounds rb = new RowBounds(offset, limit);
		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				"applicationContext.xml");
		SqlSessionFactory sf = (SqlSessionFactory) ctx
				.getBean(SqlSessionFactory.class);
		SqlSession session = sf.openSession();
		List<Meettingroom> list = session.selectList("findMeettingroom", null,
				rb);
		session.close();
		return list;
	}

	public List<Meettingroom> findEmp(Meettingroom meettingroom) {
		return mrs.findEmp(meettingroom);
	}

	public Meettingroom findMeettingroomAndBooking(Integer id) {
		return mrs.findMeettingroomAndBooking(id);
	}

}
